/*
 * 
 * Function: onClickZoomToFeature	 											   							
 * Parameters:
 * 		 Parameter1: Object															 																				
 * Return:	 
 * 		 void : 
 * 
 * Description:	Zoom to feature 
 * 
 * Author: NGUYENNV
 * Date : 2013/01/11										   												
 * 
 */
function onClickZoomToFeature(element){
	featureId = $("#txtTableHoSo").val() + "." + element.id;
	console.log(featureId);
	bound = CommonGeo.getBoundByFid(featureId);	
	map.zoomToExtent(bound, false);
	//tunglt edit	 	
	//CommonGeo.resetColorFeature(CurrentLayer.arrFeatureId);
		
	//end
	CommonGeo.setColorFeature(featureId);
}

function onClickPrev(){
	
	if(Graphic.currentPage > 0){
		if(Graphic.currentPage == Graphic.startPage)
			Graphic.startPage--;
		Graphic.currentPage--;
		Graphic.loadPaging(Graphic.currentPage);
	}
}
function onClickNext(){
	if(Graphic.currentPage == (Graphic.startPage + Graphic.displayPage - 1))
		{
			if(Graphic.currentPage < (Graphic.totalPage-1))
				{
					Graphic.startPage = Graphic.startPage + 1;
					Graphic.currentPage = Graphic.currentPage + 1;
					Graphic.loadPaging(Graphic.currentPage);
				}
		}
	else if(Graphic.currentPage < Graphic.totalPage)
		{
			Graphic.currentPage++;
			Graphic.loadPaging(Graphic.currentPage);
		}
	else{	
			// do nothing
		}
}

Graphic = 
{
		currentPage: 0, 	// Current page is displaying
		totalPage : 0,		// Total of page
		displayPage: 3,		// Num of page is displayed
		recordPerPage: 0,	// Num of record per page
		startPage: 0,
		color : "",
		divPaging: "",
		tableContent:"",
		isShowView: true,
		isShowZoom:true,
		isShowAddInfo:false,
		columId:"",
		init:function(){
			this.currentPage = 0;
			this.totalPage 	 = 0;
			this.displayPage = 3;
			this.recordPerPage = 5;
			this.startPage 	 = 0;
			this.tableContent = "";
		},
	/*
	 * 
	 * Function: comboQuanHuyen	 											   							
		 * Parameters:
		 * 		 Parameter1: Object 															 																				
		 * Return:	 
		 * 		 void : 
		 * 
		 * Description:	Draw content on popup 
		 * 
		 * Author: NGUYENNV
		 * Date : 2013/01/11										   												
		 * 
		 */
	footerInit:function(){
		$("#table-content tr").remove();
		var divId = base.getElementById('footer-page');
		base.removeChildDiv(divId);
		var prev = document.createElement("input");
		prev.type = 'button';
		prev.className = 'paging-number paging-prev';
		prev.value = 'Prev';
		$(".paging").append(prev);
		var next = document.createElement("input");
		next.type = 'button';
		next.className = 'paging-number paging-next';
		next.value = 'Next';
		$(".paging").append(next);
	},
	comboQuanHuyen:function(_ComboQ){
			
			var optionContrl1 = document.createElement("option");
			optionContrl1.value = -1;
			optionContrl1.innerHTML = LANGUAGE["da_nang"];
			optionContrl1.selected = "selected";
			_ComboQ.appendChild(optionContrl1);
			for(var i=0; i< arrFeatureDistrict.length; i++)
			{
					var optionContrl = document.createElement("option");
					optionContrl.value = arrFeatureDistrict[i].attributes["DIST_ID"];
					optionContrl.innerHTML = arrFeatureDistrict[i].attributes["NAME_VN"];
					_ComboQ.appendChild(optionContrl);
			}
		},
	/*
	 * 
	 * Function: comboPhuongXa	 											   							
		 * Parameters:
		 * 		 Parameter1: Object 															 																				
		 * Return:	 
		 * 		 void : 
		 * 
		 * Description:	Draw content on popup 
		 * 
		 * Author: NGUYENNV
		 * Date : 2013/01/11										   												
		 * 
		 */
	comboPhuongXa:function(_ComboPX,_idQuanHuyen){
			
			var optionContrl1 = document.createElement("option");
			optionContrl1.value =-1;
			optionContrl1.innerHTML = LANGUAGE["tat_ca"];
			optionContrl1.selected = "selected";
			_ComboPX.appendChild(optionContrl1);
			
			for(var i=0; i< arrFeatureCommune.length; i++)
			{
				if( arrFeatureCommune[i].attributes["DIST_ID"] == _idQuanHuyen)
					{
						var optionContrl = document.createElement("option");
						optionContrl.value = arrFeatureCommune[i].attributes["COM_ID"];
						if(arrFeatureCommune[i].attributes["NAME_VN"]==undefined)continue;
						optionContrl.innerHTML = arrFeatureCommune[i].attributes["NAME_VN"];
						_ComboPX.appendChild(optionContrl);
					}
					
			}
		},
	/*
	 * 
	 * Function: onClickPage	 											   							
		 * Parameters:
		 * 		 Parameter1: Object 															 																				
		 * Return:	 
		 * 		 void : 
		 * 
		 * Description:	Draw content on popup 
		 * 
		 * Author: NGUYENNV
		 * Date : 2013/01/11										   												
		 * 
		 */
	onClickPage:function(element){

			var pageNo = element.value;
			this.currentPage = pageNo;
			var arrayTemp = new Array();
			var begin = this.recordPerPage * pageNo;
			var end = begin + this.recordPerPage;
			ARR_LAYER_RESULT = new Array();
			for(var i = begin; i < end; i++)
				{
					if(i < CurrentLayer.arrFeatureId.length)
					arrayTemp[arrayTemp.length] = CurrentLayer.arrFeatureId[i];
				}
			ARR_LAYER_RESULT = CommonGeo.setAttributeToFidArray(arrayTemp,CurrentLayer.layerId);
			CommonFuns.FillDataToArray(ARR_LAYER_RESULT);
			Graphic.waitingData();
			this.drawFooter();
		},
	clear:function(){
			base.removeChildDiv(base.getElementById(DIV_DANH_SACH_TAI_SAN));
			//base.getElementById(DIV_MAP_CONTENT).style.display = "none";
			//base.removeChildDiv(base.getElementById(DIV_MAP_CONTENT_HEADER));
		},
	clearContent:function(){
			base.removeChildDiv(base.getElementById(DIV_DANH_SACH_TAI_SAN));
			base.removeChildDiv(base.getElementById(DIV_MAP_CONTENT_FOOTER));
	},
	/*
	 * 
	 * Function: turnOffDiv	 											   							
		 * Parameters:
		 * 		 Parameter1: void 															 																				
		 * Return:	 
		 * 		 void : 
		 * 
		 * Description:	Draw content on popup 
		 * 
		 * Author: NGUYENNV
		 * Date : 2013/01/11										   												
		 * 
		 */
	turnOffDiv:function(){
			base.removeChildDiv(base.getElementById(DIV_DANH_SACH_TAI_SAN));
			base.getElementById(DIV_MAP_CONTENT).style.display = "none";
			base.removeChildDiv(base.getElementById(DIV_MAP_CONTENT_HEADER));
			ScanFeature.removeAllFeatures();
		},
	/*
	 * 
	 * Function: createIconTurnOffDiv	 											   							
		 * Parameters:
		 * 		 Parameter1: Object 															 																				
		 * Return:	 
		 * 		 void : 
		 * 
		 * Description:	Draw content on popup 
		 * 
		 * Author: NGUYENNV
		 * Date : 2013/01/11										   												
		 * 
		 */
	createIconTurnOffDiv:function(divId){
			
			var objTable = document.createElement("table");
			objTable.border="0";
			objTable.style.width = "100%";
			var objTR = document.createElement("tr");
			var objTD = document.createElement("td");
			objTD.align = "right";
			//objTD.setAttribute("width","100%");
			var objImg = document.createElement("img");
			objImg.src = IMG_CANCEL_SRC;
			objImg.style.cursor = "pointer";
			objImg.style.padding = "0px 10px 0px 0px";
			objImg.setAttribute("onClick","Graphic.turnOffDiv()");
			objTD.appendChild(objImg);
			objTR.appendChild(objTD);
			objTable.appendChild(objTR);
			divId.appendChild(objTable);
		},
		/*
		 * 
	 * Function: drawFeaturesHeaderTable	 											   							
		 * Parameters:
		 * 		 Parameter1: Object 	
		 * 		 Parameter2: String 															 																				
		 * Return:	 
		 * 		 void : 
		 * 
		 * Description:	Draw content on popup 
		 * 
		 * Author: NGUYENNV
		 * Date : 2013/01/11										   												
		 * 
		 */
	drawFeaturesHeaderTable:function(_table, _layerName){
			
			RowHeader = document.createElement('tr');	
			cell01Header = document.createElement('td');
			cell01Header.style.fontWeight = "bold";
			cell01Header.setAttribute("align","center");
			cell01Header.innerHTML  = "STT";
			RowHeader.appendChild(cell01Header);
			for(var i=0; i < CurrentLayer.arrAlias.length;i++ )
				{
				if(CurrentLayer.arrAlias[i].IsSearch == true
						&& CurrentLayer.arrAlias[i].Name != "AnhMinhHoa" 
							&& CurrentLayer.arrAlias[i].Name != "X"
								&& CurrentLayer.arrAlias[i].Name != "Y")
					{
						cell1Header = document.createElement('td');
						cell1Header.style.fontWeight = "bold";
						cell1Header.setAttribute("align","center");
						cell1Header.id = "headCellSearch"+i;
						cell1Header.attribute = CurrentLayer.arrAlias[i].Name;
						cell1Header.innerHTML  = CurrentLayer.arrAlias[i].Alias;
						cell1Header.style.width = "200px";
						cell1Header.setAttribute("height","20px");
						RowHeader.appendChild(cell1Header);
					}
				}
			cell0Header = document.createElement('td');
			cell0Header.style.fontWeight = "bold";
			cell0Header.setAttribute("align","center");
			cell0Header.style.width = 40 +"px";
			cell0Header.innerHTML  = "Xem";
			cell01Header = document.createElement('td');
			cell01Header.style.fontWeight = "bold";
			cell01Header.setAttribute("align","center");
			cell01Header.style.width = 40 +"px";
			cell01Header.innerHTML  = "Zoom";
			//RowHeader.appendChild(cell0Header);
			RowHeader.appendChild(cell01Header);
			_table.appendChild(RowHeader);
	},
	/*
	 * 
	 * Function: drawFooter	 											   							
	 * Parameters:
	 * 		 Parameter1: Void 															 																				
	 * Return:	 
	 * 		 void : 
	 * 
	 * Description:	Draw content on popup 
	 * 
	 * Author: NGUYENNV
	 * Date : 2013/01/11										   												
	 * 
	 */
	drawFooter:function(){
		
		this.footerInit();
		
		var num = CurrentLayer.arrFeatureId.length / this.recordPerPage;
		var round = Math.round(num);
		if(num > round) num = round + 1;
		else num = round;
		this.totalPage = num;
		var display = 1;
		if((this.totalPage - this.currentPage - 1) >= this.displayPage)
			display = this.displayPage;
		else
			display = this.totalPage - this.currentPage;
		
		for(var i= this.currentPage ; i < this.totalPage; i++){

			var input = document.createElement("input");
			input.type = 'button';
			input.className = 'paging-number';
			input.value = i;
			input.setAttribute("onClick","Graphic.onClickPage(this)");
			$('.paging-next').before(input);
		}
	},
	drawFooter_:function(){
		
		var num = CurrentLayer.arrFeatureId.length / this.recordPerPage;
		var round = Math.round(num);
		if(num > round) num = round + 1;
		else num = round;
		this.totalPage = num;
		
		var divId = base.getElementById(DIV_MAP_CONTENT_FOOTER);
		base.removeChildDiv(divId);
		var objTable = document.createElement("table");
		objTable.border="0";
		objTable.style.width = "100%";
		var objTR = document.createElement("tr");
		var objTD = document.createElement("td");
		objTD.setAttribute("BGCOLOR",this.color);
		objTD.style.padding = "0px 5px 0px 5px";
		objTR.appendChild(objTD);
		objTD.align = "right";
		objTD.innerHTML =  "Trang "+(this.currentPage+1) +" trong "+this.totalPage;
		objTD.setAttribute("width","90%");
		objTable.appendChild(objTR);
		divId.appendChild(objTable);
		
		var display = 1;
		
		if((this.totalPage - this.currentPage - 1) >= this.displayPage)
			
			display = this.displayPage;
		else
			display = this.totalPage - this.currentPage;
		
		for(var i= this.currentPage ; i < (this.currentPage + display); i++){
			
			var objTD0 = document.createElement("td");
			objTD0.setAttribute("width","10px");
			objTD0.align = "center";
			objTD0.setAttribute("BGCOLOR",this.color);
			var lable = document.createElement("label");
			
			
			if(this.currentPage == 0)
			{	
				objTR.appendChild(objTD0);
				lable.innerHTML =  i+1;
				lable.value = i;
				lable.setAttribute("onClick","Graphic.onClickPage(this)");
				objTD0.appendChild(lable);
				
				if(i == ((this.currentPage + display)-1))
				{
					if(this.displayPage < (this.totalPage-1))
					{
						var objTD12 = document.createElement("td");
						objTD12.setAttribute("width","15px");
						objTD12.innerHTML =  "...";
						objTR.appendChild(objTD12);
					}
				}
			}
			else 	
			{
				if(i == this.currentPage)
				{
					var objTD2 = document.createElement("td");
					objTD2.setAttribute("width","20px");
					objTD2.setAttribute("BGCOLOR",this.color);
					var lable1 = document.createElement("label");
					lable1.innerHTML =  LANGUAGE["start"];;
					lable1.value = 0;
					lable1.setAttribute("onClick","Graphic.onClickPage(this)");
					objTD2.appendChild(lable1);
					objTR.appendChild(objTD2);
					
					var objTD1 = document.createElement("td");
					objTD1.setAttribute("BGCOLOR",this.color);
					objTD1.setAttribute("width","10px");
					objTD1.innerHTML =  "...";
					objTR.appendChild(objTD1);
					
					lable.innerHTML =  i+1;
					lable.value = i;
					lable.setAttribute("onClick","Graphic.onClickPage(this)");
					objTD0.appendChild(lable);
				}
				else
				{
					lable.innerHTML =  i+1;
					lable.value = i;
					lable.setAttribute("onClick","Graphic.onClickPage(this)");
					objTD0.appendChild(lable);
				}
				objTR.appendChild(objTD0);
			}
		}
		
	},
	/*
	 * 
	 * Function: drawBodyTable	 											   							
	 * Parameters:
	 * 		 Parameter1: Object
	 *  	 Parameter2: Array												 																				
	 * Return:	 
	 * 		 void : 
	 * 
	 * Description:	Draw content on popup 
	 * 
	 * Author: NGUYENNV
	 * Date : 2013/01/11										   												
	 * 
	 */
	drawBodyTable:function(_table, _arrray){

		for(var j=0; j<_arrray.length; j++)	
		{
			RowHeader = document.createElement('tr');
			cellNum = document.createElement('td');
			cellNum.setAttribute("align","center");
			cellNum.innerHTML  = (this.currentPage * this.recordPerPage)+j;
			RowHeader.appendChild(cellNum);
		
				for(var i=0; i < CurrentLayer.arrAlias.length;i++ )
					{
				
					if(CurrentLayer.arrAlias[i].IsSearch == true 
							&& CurrentLayer.arrAlias[i].Name != "AnhMinhHoa"
								&& CurrentLayer.arrAlias[i].Name != "X"
									&& CurrentLayer.arrAlias[i].Name != "Y")
						{
							cell1Header = document.createElement('td');
							if(CurrentLayer.arrAlias[i].IsCategory){
								cell1Header.setAttribute("align","left");
							}
							else if(CurrentLayer.arrAlias[i].Type == "int" 
								|| CurrentLayer.arrAlias[i].Type == "decimal")
								{
									cell1Header.setAttribute("align","right");
								}
							else if(CurrentLayer.arrAlias[i].Type == "text" 
								|| CurrentLayer.arrAlias[i].Type == "bigtext")
									cell1Header.setAttribute("align","left");
							else	
							cell1Header.setAttribute("align","center");
							cell1Header.style.padding = "0px 5px 0px 5px";
							cell1Header.id = "headCellSearch"+i;
							cell1Header.attribute = CurrentLayer.arrAlias[i].Name;
							value = _arrray[j].attribute[CurrentLayer.arrAlias[i].Name];
							if(value == null||value==undefined) value = "";
							cell1Header.innerHTML  = value;
							cell1Header.setAttribute("height","25px");
							RowHeader.appendChild(cell1Header);
						}
					}
				/*cell0Header = document.createElement('td');
				cell0Header.setAttribute("align","center");
				imgView = document.createElement("img");
				imgView.id = "view"+_arrray[j].attribute["fid"];
				imgView.attribute = _arrray[j].attribute["fid"];
				imgView.setAttribute("onClick","onClickDetailInfor(this)");
				imgView.setAttribute("src",IMG_VIEW_SRC);
				cell0Header.appendChild(imgView);*/
				
				TDZoom = document.createElement('td');
				TDZoom.setAttribute("align","center");
				imgZoom = document.createElement("img");
				imgZoom.id = "zoom"+_arrray[j].attribute["fid"];
				imgZoom.attribute = _arrray[j].attribute["fid"];
				imgZoom.setAttribute("onClick","onClickZoomToFeature(this)");
				imgZoom.setAttribute("src",IMG_ZOOM_SRC);
				TDZoom.appendChild(imgZoom);
				
				//RowHeader.appendChild(cell0Header);
				RowHeader.appendChild(TDZoom);
				_table.appendChild(RowHeader);
		}
	},
	/*
	 * 
	 * Function: waitingData	 											   							
	 * Parameters:
	 * 		 Parameter1: Void 															 																				
	 * Return:	 
	 * 		 void : 
	 * 
	 * Description:	Draw content on popup 
	 * 
	 * Author: NGUYENNV
	 * Date : 2013/01/11										   												
	 * 
	 */
	waitingData:function(){
		//var divId = base.getElementById(DIV_DANH_SACH_TAI_SAN);
		var objRead = null;
		if (COMPLETED_WAITING != IS_READING_DATA ) {
			//divId.innerHTML = "Loading...";
			objRead = setTimeout("Graphic.waitingData()", 100);
		} 
		else {
			clearTimeout(objRead);
			Graphic.clear();
			var objTable = base.getElementById(this.tableContent);
			//var objTable = document.createElement("table");
			objTable.setAttribute("bordercolor",COLOR_BORDER);
			objTable.border="1";
			objTable.style.width="100%";
			Graphic.drawFeaturesHeaderTable(objTable, CurrentLayer.layerId);
			Graphic.drawBodyTable(objTable,ARR_LAYER_RESULT);
		}
	},
	/*
	 * 
	 * Function: displayList	 											   							
	 * Parameters:
	 * 		 Parameter1: Array 															 																				
	 * Return:	 
	 * 		 void : 
	 * 
	 * Description:	Display list of array 
	 * 
	 * Author: NGUYENNV
	 * Date : 2013/01/11										   												
	 * 
	 */
	displayList:function(_array){

		var layerId = CurrentLayer.layerId;
		CurrentLayer.arrFeatureId = _array;
		this.currentPage = 0;
		this.startPage = 0;
		ARR_LAYER_RESULT = new Array();
		SearchingAttribute.arrResult = new Array();
		if(CurrentLayer.arrFeatureId.length > this.recordPerPage)
			{
				var layer = LayerTool.getLayerFromLayerID(layerId);
				for(var j=0;j<_array.length;j++){
					var _id = _array[j];
					for(var i=0;i<layer.features.length;i++){
						var fid = layer.features[i].fid;
						if(fid==_id){
							SearchingAttribute.arrResult[SearchingAttribute.arrResult.length] = layer.features[i].attributes[Graphic.columId];
						}
					}
				}
				var arrayTemp = new Array();
				for(var i = 0; i < this.recordPerPage; i++)
					{
						arrayTemp[i] = CurrentLayer.arrFeatureId[i];
					}
				ARR_LAYER_RESULT = CommonGeo.setAttributeToFidArray(arrayTemp,layerId);
			}
		else
			{
				//SearchingAttribute.arrResult = CommonGeo.setAttributeToFidArray(_array,layerId);
				ARR_LAYER_RESULT = CommonGeo.setAttributeToFidArray(_array,layerId);
				for(var j=0;j<ARR_LAYER_RESULT.length;j++){
					SearchingAttribute.arrResult[SearchingAttribute.arrResult.length] = ARR_LAYER_RESULT[j].attribute[Graphic.columId];
				}
			}
		CommonFuns.FillDataToArray(ARR_LAYER_RESULT);
	},
	addZoomTo:function(objectRow, featureId){
		TDZoom = document.createElement('td');
		TDZoom.setAttribute("align","center");
		imgZoom = document.createElement("img");
		imgZoom.id = "zoom"+featureId;
		imgZoom.attribute = featureId;
		imgZoom.setAttribute("onClick","onClickZoomToFeature(this)");
		imgZoom.setAttribute("src",IMG_ZOOM_SRC);
		TDZoom.appendChild(imgZoom);
		objectRow.appendChild(TDZoom);
	},
	addRowViewDetail:function(objectRow, featureId){
		
		cell0Header = document.createElement('td');
		cell0Header.setAttribute("align","center");
		imgView = document.createElement("img");
		imgView.id = "view"+ featureId;
		imgView.attribute = featureId;
		imgView.setAttribute("onClick","onClickDetailInfor(this)");
		imgView.setAttribute("src",IMG_VIEW_SRC);
		cell0Header.appendChild(imgView);
		objectRow.appendChild(cell0Header);
	}
};